// 递归获取数据树中 从子找父查找id的属性
export function findFieldFromTree(treeData, field = 'id', arr = []) {
    for (let key in treeData) {
        if (treeData && treeData.data && treeData.data[field]) {
            arr.unshift(treeData.data[field])
        }
        if (treeData?.parent) {
            return findFieldFromTree(treeData.parent, field, arr)
        }
    }
    return arr
}

// 组装树数据
export function makeTreeData(list) {
    let arr = [];
    if (list.length !== 0) {
        const map = new Map();
        for (const item of list) {
            map.set(item.id, item);
        }

        for (const node of list) {
            if (node.pid == "") {
                arr.push(node);
            } else {
                let item = map.get(node.pid);
                item.children.push(node)
            }
        }
    }
    return arr
}

// 判断是否为空对象
export function isNotEmptyObject(obj) {
    return obj && typeof obj === 'object' && !Array.isArray(obj) && Object.keys(obj).length > 0;
}

// 对象数组 根据对象属性 查找数组中对应的对象
export function getItemByProp(arr, propKey, fieldKey) {
    let obj = {}
    if (arr.length != 0) {
        obj = arr.find(item => item[propKey] == fieldKey)
    }
    return obj
}